*This do file augments CFS's "Turnout_District_level.do" to include information on women's participation. Since women's info is at the constituency level, but the pre-reform data from CFS was municipality level, the collapse will not sum the women's values. 
************************************************************
*Look at 0.Read Me for details about the merging process
*Download CFS (2016) replication files. 
*run panel_all_women.do >creates panel_all_women.dta
*then run Turnout_District_Level.do > creates collapsed data. 
************************************************************



cd "YOUR DIRECTORY HERE"
use panel_all_women.dta, clear


/* only for descriptives  */

gen totalvotes_first_CFS=totalvotes_mun if round==1
replace totalvotes_first_CFS=. if round==2


/* MAKING FINAL ROUND VARIABLES pre-reform */

gen turnout_mun_final_CFS=turnout_mun
replace turnout_mun_final_CFS=fut_turnout if fut_turnout!=.   /* replacing with second round turnout, if that exists */

la var turnout_mun_final_CFS "Municipal turnout, final round, CFS measure"

gen totalvotes_final_CFS=totalvotes_mun
replace totalvotes_final_CFS=fut_totalvotes_mun if fut_totalvotes_mun!=. 
la var totalvotes_final_CFS "Municipal votes cast, final round, CFS measure"
  /* replacing with second round turnout, if that exists */
*DT: CFS says they use turnout in the final round of voting and that it is approved votes/eligible voters. The construction of panel_all is not in their replication files so we cannot verify. 
 

*****************************
**Party vote shares 
*****************************

*party vote shares, keeping the final round tallies. 


*Parties listed 1905-1918: 
*votes1S votes1A votes1V_A votes1V votes1L votes1T votes1KIR votes1FV votes1H_FV votes1H votes1SPR votes1OTH

*CFS classified as other: votes1T votes1KIR votes1SPR 


forval x=0/1{
local r=`x'+1

egen lib_v`r' = rowtotal(votes`r'V votes`r'A votes`r'V_A) if hadsecond==`x'
egen soc_v`r' = rowtotal (votes`r'S) if hadsecond==`x'
egen con_v`r' = rowtotal (votes`r'FV votes`r'H votes`r'H_FV votes`r'L ) if hadsecond==`x'
egen other_v`r' = rowtotal (votes`r'OTH votes`r'T votes`r'KIR votes`r'SPR)
}

replace lib_v1=lib_v2 if hadsecond==1
replace soc_v1=lib_v2  if hadsecond==1
replace con_v1=con_v2 if hadsecond==1
replace other_v1=other_v2 if hadsecond==1

foreach word in lib soc con other {
drop `word'_v2 
ren `word'_v1 `word'_final
}

******************************************

/* KEEPING ONLY ONE ROUND OF MUNICIP ELECTIONS AND MERGING PR PERIOD */
keep if round==1

append using storting1921_municipality
append using storting1924_municipality
append using storting1927_municipality
append using storting1930_municipality
append using storting1933_municipality
append using storting1936_municipality


/*DT: in the PR data the government differentiate between mailed in votes and personal votes personal votes + mail votes = casted votes, SMD data doesn't differentiate. Teele (2022) looks at the total. */ 


/* CREATING DISTRICT LEVEL VARIABLES */

egen SMD_district=max(valgkrets), by(knr)   /* valgkrets is between 101 and 2002 pre-reform */
egen PR_district=min(valgkrets), by(knr)   /* valgkrets is between 1 and 31 post-reform */

** oslo (5) knr=301, drammen (2) knr=602, kristiansand (2) knr=1001, stavanger (2) knr=1103, bergen (4) knr=1301, trondheim (4) knr=1601
** ... consist of multiple districts within a municipality. We do not have the possibility to get post-reform data on that level
** ... so we lose these 19 districts. Leaving us with 126-19-12=95 districts.
** some municipalities are split after reform , need to manually find their SMD_district 

sort knr year
list knr SMD_district if SMD_district<100


replace SMD_district=101 if knr==126 /* 126 mysen was part of 125 eidsberg up to 1920 */
replace SMD_district=203 if knr==213 /* 213 ski was part of 212 krÂkstad up to 1931 */
replace SMD_district=203 if knr==222 /* 222 nordre h¯land was part of 221 h¯land */
replace SMD_district=203 if knr==225
replace SMD_district=203 if knr==228
*replace SMD_district=XXX if knr==301  /* oslo 5 SMDS (becomes 1 PR district )*/
replace SMD_district=406 if knr==435
*replace SMD_district=XXX if knr==602 /* drammen 2 SMDS */
replace SMD_district=902 if knr==917
*replace SMD_district=XXX if knr==1001 /*Kristianstand 2 SMDS */ 
*replace SMD_district=XXX if knr==1103 /*Stavenger 2 SMDS */ 
replace SMD_district=1103 if knr==1125
replace SMD_district=1103 if knr==1127
replace SMD_district=1103 if knr==1144
replace SMD_district=1104 if knr==1151
replace SMD_district=1105 if knr==1156
replace SMD_district=1105 if knr==1158
replace SMD_district=1204 if knr==1256
replace SMD_district=1204 if knr==1260
replace SMD_district=1204 if knr==1262
*replace SMD_district=XXX if knr==1301 /*Bergen 4 SMDS*/ 
replace SMD_district=1404 if knr==1435
replace SMD_district=1404 if knr==1436
replace SMD_district=1502 if knr==1518
replace SMD_district=1504 if knr==1547
*replace SMD_district= if knr==1601   /*Trondheim (4 SMDS)*/ 
replace SMD_district=1602 if knr==1611
replace SMD_district=1602 if knr==1613
replace SMD_district=1603 if knr==1637
replace SMD_district=1603 if knr==1639
replace SMD_district=1604 if knr==1641
replace SMD_district=1604 if knr==1642
replace SMD_district=1604 if knr==1643
*replace SMD_district= if knr==1701  /*Levanger --> is this also multiple districts? */
replace SMD_district=1703 if knr==1739
replace SMD_district=1703 if knr==1740
replace SMD_district=1703 if knr==1741
replace SMD_district=1801 if knr==1801
replace SMD_district=1802 if knr==1803
replace SMD_district=1802 if knr==1823
replace SMD_district=1802 if knr==1825
replace SMD_district=1802 if knr==1829
replace SMD_district=1802 if knr==1831
replace SMD_district=1804 if knr==1854 /*DT: This is one of the districts with a mismatch of approved votes to final votes */
replace SMD_district=1805 if knr==1856
replace SMD_district=1805 if knr==1861
replace SMD_district=1805 if knr==1863
replace SMD_district=1806 if knr==1869
replace SMD_district=1806 if knr==1871
replace SMD_district=1806 if knr==1873
replace SMD_district=1901 if knr==1912
replace SMD_district=1901 if knr==1913
replace SMD_district=1902 if knr==1916
replace SMD_district=1902 if knr==1918
replace SMD_district=1902 if knr==1919
replace SMD_district=1903 if knr==1923
replace SMD_district=1904 if knr==1939
replace SMD_district=1904 if knr==1940


/*type 1 "Included" 2 "Large Muni, Multiple SMDS" 3 "Added reshuffled Municipalities or split over PR districts " 

Cox et al. drop  types 2 and 3 because I'm going to do that before collapsing but not here, so just ignoring the following commands 

drop if SMD_district<100  /* these are the municipalities split over multiple PR districts post-reform */
drop if SMD_district>3000  /* these are the multiple-districts municipalities pre-reform */

/* Lillehammer, Hamar, Gj¯vik og Kongsvinger delt i to */
drop if valgkrets==400 
drop if valgkrets==490
drop if valgkrets==500

/* extra district was added in Romsdal, and then municipalities appear to be reshuffled from 1915 to 1918 */
drop if valgkrets==1501
drop if valgkrets==1502
drop if valgkrets==1503
drop if valgkrets==1504
drop if valgkrets==1505
drop if valgkrets==1506

/* extra district was added in Troms, and then municipalities appear to be slightly reshuffled from 1915 to 1918 */
drop if valgkrets==1901
drop if valgkrets==1902
drop if valgkrets==1903
drop if valgkrets==1904


drop if knr==1280 /* aarstad part of Bergen from 1.7.1915 */

drop if SMD_district==690  /* h¯nefoss and notodden belongs to different post-reform districts (and counties) */
drop if SMD_district==892  /* brevik and holmestrand belongs to different post-reform districts (and counties) */
*/ 

gen turnout_CFS=.
replace turnout_CFS=turnout_mun_final if year<1920
replace turnout_CFS=approvedvotesoverall/eligibleoverall if year>1920
la var turnout_CFS "Turnout, approved/eligible, CFS Measure" 

drop _merge
sort knr


*here I need to merge the SMD district levels with the PR. e.g. 






save Turnout_District_Level_Before_Collapse, replace




****************************************************************************
/*Merge in the SMD district data for the large cities just for the year 1918.*/ 

****************************************************************************
 
use Turnout_District_Level_Before_Collapse, clear

drop if SMD_district<100  /* these are the municipalities split over multiple PR districts post-reform */

/*DT: casted votes are the sum of personal votes and mail votes in the post PR period*/ 

egen castedvoteswomen_post=rowtotal(personalvotewomen mailwomen) /*this is post-reform data, so municipality level */  
egen castedvotesmen_post=rowtotal(personalvotesmen mailmen) /*this is post-reform data, so municipality level */ 
insp castedvoteswomen_post /*there are 2,257 that are zero because the pre-reform data do not have the variables personalvotewomen or mailwomen*/ 
 
 *the women data are already at the valgrets level and not at the level of the municipality so for them we do not sum. Same thing for the vote tallies from before 1918 
order votes*

collapse (mean) PR_district (mean) margin (sum) eligibleoverall eligiblewomen eligiblemen (sum) approvedvotesoverall rejectedvotesoverall castedvoteswomen_post castedvotesmen_post (sum) totalvotes_first  votes* (max)  soc lib con other eligiblemen_final castedmen_final turnoutmen_final eligiblewomen_final castedwomen_final turnoutwomen_final eligibleoverall_final castedoverall_final turnoutoverall_final approved_final, by(SMD_district year)


******Party data after 1921 

*Parties listed for 1921
* votesDNA (Det norske Arbeiderparty, Labour party) 
*votesNSA (Norges Socialdemokratiske abeiderparti, social democratic workers party broke away from labour in 1919 and merged again in 1927) 
*votesRF Radikale Folkeparti, radical people's party, local farmer party, liberal and semi-socialist
*votesV Venstre, Liberal 
*votesSP Norsk Landmandsforbund, agrarian league, centre 
*votesH_FV Hoire, Frisindede venstre, Right, with liberal left. 
*votesOTH Andre lister 
replace lib_final=  votesV+votesSP if year==1921
replace soc_final= votesDNA+ votesNSA +votesRF if year==1921
replace con_final= votesH_FV if year==1921
replace other_final=votesOTH if year==1921

*Parties listed for 1924, same as above for 1921 plus three more
*votesNKP Norges Kommunistiske Parti 
*votesFV Frisindede Venstre Venstre, liberal left > Part of the Right, or conservative from 1909-1930
*votesVIL Ville Stemmer ? Can't find, put in other 

replace lib_final=  votesV+votesSP  if year==1924
replace soc_final= votesDNA+ votesNSA +votesRF + votesNKP if year==1924
replace con_final= votesH_FV + votesFV if year==1924
replace other_final=votesOTH + votesVIL if year==1924


*Parties listed for 1933 included the 1921 plus 

*votesMORE more fisker og smabrukerparti put in other 
*votesSAM samfundsparteit (sociaist party)
*votesNS nasjonal samling National coalititon (put in other, only two elections) 


replace lib_final=  votesV+votesSP  if year==1933
replace soc_final= votesDNA+ votesNSA +votesRF + votesNKP + votesSAM if year==1933
replace con_final= votesH_FV + votesFV if year==1933
replace other_final=votesOTH + votesVIL +votesNS + votesMORE if year==1933

*drop votes*

*combine pre and post 


replace eligibleoverall=eligibleoverall_final if year<1920
replace eligiblewomen=eligiblewomen_final if year<1920 
replace eligiblemen =eligiblemen_final if year<1920
replace approvedvotesoverall=approved_final if year<1920
la var eligibleoverall "Eligible Voters, panel" 
la var eligiblewomen "Eligible Women, panel"
la var eligiblemen "Eligible Men, panel" 
la var approvedvotesoverall "Approved Votes, panel" 

gen castedvoteswomen=castedvoteswomen_post
replace castedvoteswomen=castedwomen_final if year<1920
la var castedvoteswomen "Women's Votes Cast, panel" 

gen castedvotesmen=castedvotesmen_post
replace castedvotesmen=castedmen_final if year<1920
la var castedvotesmen "Men's Votes Cast, panel" 

gen castedvotesoverall=castedvotesmen+castedvoteswomen if year>=1920
replace castedvotesoverall=castedoverall_final if year<1920
la var castedvotesoverall "Total Votes Cast, panel" 

*DT:I verified that collapsing the pre-reform women data using the firstnm or the max produced the same results.


assert eligibleoverall>eligiblewomen
*this works on March 28 2022

*bysort PR_district: tab SMD_district
*we will only have this for the PR years right now 

*May 20: this casted versus approved is a problem. 
gen turnout_CFS=approvedvotesoverall/eligibleoverall
la var turnout_CFS "Turnout, approved/eligible, CFS Measure" 

gen turnout_DT=castedvotesoverall/eligibleoverall
gen turnoutwomen_DT=castedvoteswomen/eligiblewomen
gen turnoutmen_DT=castedvotesmen/eligiblemen
gen womensshareturnout_DT=castedvoteswomen/castedvotesoverall

la var turnout_DT "Turnout, cast/eligible"
la var turnoutwomen_DT "Women's Turnout" 
la var turnoutmen_DT "Men's Turnout" 
la var womensshareturnout_DT "Women's Share of Votes Cast" 



xtset SMD_district year  /* DT: this tells stata that it is a panel dataset and that the SMD_district year is the unique identifier */ 

gen xx=1
bysort SMD_district: egen zz=total(xx)
keep if zz==10


order year SMD_district PR_district margin eligibleoverall eligiblemen eligiblewomen approvedvotesoverall rejectedvotesoverall castedvoteswomen castedvotesmen castedvotesoverall turnout_DT turnoutwomen_DT turnoutmen_DT womensshareturnout_DT

drop xx zz

************
/*DT: preserve basically performs a function on the data and then acts as if it weren't done when the word restore comes back up */ 

preserve
use "CFS/Storting1921",clear
gen PR_district=valgkrets
gen magnitude=rep
gen electorate1921=eligibleoverall       /* this is electorate in first PR election */
collapse magnitude electorate, by(PR_district)
sort PR_district
save "CFS/magnitudePR",replace
restore
*now we are back in the turnout dataset, and they merge back in the district magniude dataset from Storting1921 that they just created. 
sort PR_district
merge m:1 PR_district using "CFS/magnitudePR.dta", gen(_mmagnitudePR)
*************

replace magnitude=1 if year<1920

/* creating var. for electorate based on the PR districts */
bysort PR_district year: egen electoratePR=sum(eligibleoverall)
bysort PR_district year: egen electoratePRwomen=sum(eligiblewomen)


gen voters_per_MP=eligibleoverall/magnitude


drop if year==.
sort SMD_d year
*browse year SMD_district PR_district magnitude eligibleoverall electoratePR electorate voters_per_MP


/* CREATING VARIOUS VARIABLES */

gen SMD=0
replace SMD=1 if year<1920

bysort SMD_district: egen SMD_margin_pre=mean(margin)  /* AVG. DIFFERENCE BETWEEN WINNER AND RUNNER UP IN FIRST ROUND IN SMD PERIOD */
xtile SMD_margin_quantile=SMD_margin_pre, nq(2)
tab SMD_margin_quantile, gen(d_SMD_margin_quantile)

bysort SMD_district: egen magnitude_post=max(magnitude)  /* post-reform magnitude */

sort SMD_district year

/*DT: this "l3" is using the third lag of the variable turnout because elections are 3 years apart*/ 
gen ch_turnout_CFS=turnout_CFS-l3.turnout_CFS 
gen ch_turnout_DT=turnout_DT-l3.turnout_DT
gen ch_turnoutmen_DT=turnoutmen_DT-l3.turnoutmen_DT 
gen ch_turnoutwomen_DT=turnoutwomen_DT-l3.turnoutwomen_DT 
gen ch_votescast=totalvotes_first-l3.totalvotes_first
gen ch_votescastmen=castedvotesmen-l3.castedvotesmen
gen ch_votescastwomen=castedvoteswomen-l3.castedvoteswomen

gen ch_magnitude=magnitude-l3.magnitude
gen ch_voters_per_MP=(voters_per_MP-l3.voters_per_MP)/1000

gen d_SMD_margin_quantile2Xch_voters=d_SMD_margin_quantile2*ch_voters

drop d_SMD_margin_quantile1  /*reference category */

*tab year, gen(d_year)
*tab SMD_district, gen(d_SMD_district)

gen close=SMD_margin_pre<0.149
gen reform=year>1920
gen small=magnitude_post<5.5

gen closeXsmall=close*small
gen closeXreform=close*reform
gen smallXreform=small*reform
gen closeXsmallXreform=close*small*reform

keep if year<1930 /* electoral cartels introduced */
*save Turnout_District_Level, replace
save Turnout_District_Level_ladies, replace






